package com.hjwoo.app.adpter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.hjwoo.app.R;
import com.hjwoo.app.been.OrdersObject;
import com.hjwoo.app.utils.LoanLog;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;
import com.hjwoo.app.views.AppointmentActivity;
import com.hjwoo.app.views.ImmediatelyActivity;
import com.hjwoo.app.views.TextActivity;

import java.util.List;
import java.util.TimeZone;

public class LeadsLVAdapter extends BaseAdapter {
    Context context;
    // 定义一个视图容器
    ViewHolder holder;
    // 定义一个商品集合
    List<OrdersObject> Orders;
    // 用来导入布局
    private LayoutInflater inflater = null;
    private int flag1 = 0;//立即报单
    private int flag2 = 0;//立即预约

    private String description;

    //    public LeadsLVAdapter(Context context)
//    {
//        super();
//        this.context = context;
//        inflater = LayoutInflater.from(context);
//    }
    public LeadsLVAdapter(Context context, List<OrdersObject> Orders) {
        super();
        this.context = context;
        this.Orders = Orders;
        inflater = LayoutInflater.from(context);
        // 创建bitmap
    }

    @Override
    public int getCount() {
        return Orders.size();
    }

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        // 定义一个视图容器
        holder = null;
        if (convertView == null) {
            // 获取ViewHolder对象
            holder = new ViewHolder();
            // 导入布局并赋值给convertview
            convertView = inflater.inflate(R.layout.item_leads_lv, null);
            holder.serialNumber = (TextView) convertView
                    .findViewById(R.id.serialNumber);
            holder.createdDate = (TextView) convertView
                    .findViewById(R.id.createdDate);
            holder.status = (TextView) convertView
                    .findViewById(R.id.status);
            holder.status2 = (TextView) convertView
                    .findViewById(R.id.status2);
            holder.status3 = (TextView) convertView
                    .findViewById(R.id.status3);
            holder.status4 = (TextView) convertView
                    .findViewById(R.id.status4);
            holder.status5 = (TextView) convertView
                    .findViewById(R.id.status5);
            holder.address = (TextView) convertView
                    .findViewById(R.id.address);
            holder.addressleft = (TextView) convertView
                    .findViewById(R.id.addressleft);
            holder.stage = (TextView) convertView
                    .findViewById(R.id.stage);
            holder.result = (TextView) convertView
                    .findViewById(R.id.result);
            holder.baodan = (Button) convertView
                    .findViewById(R.id.baodan);
            convertView.setTag(holder);
        } else {
            // 取出holder
            holder = (ViewHolder) convertView.getTag();
        }

//        按钮初始化设置成隐藏
        holder.baodan.setVisibility(View.GONE);

//        if (Orders.get(position).getStage().getName().equals("评房已完成")) {
//            holder.baodan.setText("立即报单");
//            holder.baodan.setVisibility(View.VISIBLE);
//            flag1 = 1;
//        } else if (Orders.get(position).getStage().getName().equals("房产初审已完成")) {
//            holder.baodan.setText("立即预约");
//            holder.baodan.setVisibility(View.VISIBLE);
//            flag2 = 1;
//        } else if (Orders.get(position).getStage().getName().equals("报单申请已提交") || Orders.get(position).getStage().getName().equals("基础材料已提供") ||
//                Orders.get(position).getStage().getName().equals("产调已完成") || Orders.get(position).getStage().getName().equals("征信查询已完成")||Orders.get(position).getStage().getName().equals("信息已完善")) {
//            holder.baodan.setVisibility(View.GONE);
//        } else if (Orders.get(position).getStage().getName().equals("预约申请已提交") ||
//                Orders.get(position).getStage().getName().equals("预约已指派") || Orders.get(position).getStage().getName().equals("产调已完成") ||
//                Orders.get(position).getStage().getName().equals("征信查询已完成") || Orders.get(position).getStage().getName().equals("下户已完成")) {
//            holder.baodan.setVisibility(View.GONE);
//        } else if (Orders.get(position).getStage().getName().equals("审批已完成") || Orders.get(position).getStage().getName().equals("抵押公证手续已完成") ||
//                Orders.get(position).getStage().getName().equals("放款已完成")) {//已审批
//            holder.baodan.setVisibility(View.GONE);
//        } else if (Orders.get(position).getStage().getName().equals("返点已完成")) {
//            holder.baodan.setVisibility(View.GONE);
//        } else if (Orders.get(position).getStatus().equals("Failed")) {
//            holder.baodan.setVisibility(View.GONE);
//        }
        holder.baodan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent;
                if (Orders.get(position).getStage().getName().equals("评房已完成")) {
                    intent = new Intent(context, ImmediatelyActivity.class);
                    intent.putExtra("serialNumber", Orders.get(position).getSerialNumber());
                    context.startActivity(intent);
                } else if (Orders.get(position).getStage().getName().equals("房产初审已完成")) {
                    intent = new Intent(context, AppointmentActivity.class);
                    Bundle mBundle = new Bundle();
                    mBundle.putSerializable("order", Orders.get(position));//传递某个单子的信息
                    intent.putExtras(mBundle);
                    context.startActivity(intent);
                }
            }
        });

//        将必带材料等设置为隐藏
        holder.status2.setVisibility(View.GONE);
        holder.status3.setVisibility(View.GONE);
        holder.status4.setVisibility(View.GONE);
        holder.status5.setVisibility(View.GONE);

        if (Orders.get(position).getCity() != null) {

//        设置房产地址
            holder.address.setText(Orders.get(position).getCity().getName() + ";" + Orders.get(position).getBuilding() + ";" + Orders.get(position).getFloor() + ";" + Orders.get(position).getTotalFloor() + ";" +
                    (Orders.get(position).getRoomNumber() ==null ? "" : Orders.get(position).getRoomNumber()) + ";" + Orders.get(position).getOrientation() + ";" + Orders.get(position).getArea() + "㎡" + ";" + Orders.get(position).getHouseType().getName() + ";" +
                    (Orders.get(position).getSpecialFactors().getName().equals("无") ? "" : Orders.get(position).getSpecialFactors().getName()));
//            holder.address.setText(Orders.get(position).getCity().getName() + ";" + Orders.get(position).getBuilding() + ";" + Orders.get(position).getFloor() + ";" + Orders.get(position).getTotalFloor() + ";" +
//                    Orders.get(position).getRoomNumber() + ";" + Orders.get(position).getOrientation() + ";" + Orders.get(position).getArea() + "㎡" + ";" + Orders.get(position).getHouseType().getName() + ";" +
//                    (Orders.get(position).getSpecialFactors().getName().equals("无") ? "" : Orders.get(position).getSpecialFactors().getName()));
        }

        if (Orders.get(position).getStage().getName().equals("评房已完成")) {
            holder.stage.setText("已评房");
            holder.result.setText("评房结果:");
            //评房结果
            holder.status.setText((int) (Double.valueOf(Orders.get(position).getLoanAmount()) * 0.9) + "~" + (int) (Double.valueOf(Orders.get(position).getLoanAmount()) * 1) + "万元");

            holder.baodan.setText("立即报单");
            holder.baodan.setVisibility(View.VISIBLE);
//            holder.status.setText(Orders.get(position).getLoanAmount() + "万元");

        } else if (Orders.get(position).getStage().getName().equals("报单申请已提交") || Orders.get(position).getStage().getName().equals("基础材料已提供") || Orders.get(position).getStage().getName().equals("信息已完善")) {
            holder.stage.setText("已报单");
            holder.result.setText("评房结果:");
            //评房结果

            if (Orders.get(position).getCity() != null) {
                holder.status.setText((int) (Double.valueOf(Orders.get(position).getLoanAmount()) * 0.9) + "~" + (int) (Double.valueOf(Orders.get(position).getLoanAmount()) * 1) + "万元");
//            holder.status.setText(Orders.get(position).getLoanAmount() + "万元");

            } else {
                holder.result.setText("借款人姓名：");
                holder.status.setText(Orders.get(position).getFullName());
                holder.addressleft.setText("婚姻状态：");
                holder.address.setText(Orders.get(position).getMaritalStatus());

//                holder.address.setText(Orders.get(position).getCity().getName() + ";" + Orders.get(position).getBuilding() + ";" + Orders.get(position).getFloor() + ";" + Orders.get(position).getTotalFloor() + ";" +
//                        Orders.get(position).getRoomNumber() + ";" + Orders.get(position).getOrientation() + ";" + Orders.get(position).getArea() + "㎡" + ";" + Orders.get(position).getHouseType().getName() + ";" +
//                        (Orders.get(position).getSpecialFactors().getName().equals("无") ? "" : Orders.get(position).getSpecialFactors().getName()));
            }


        } else if (Orders.get(position).getStage().getName().equals("房产初审已完成")) {
            holder.stage.setText("已初审");
            holder.result.setText("初审结果：");

//            隐藏立即报单
//            holder.baodan.setText("立即预约");
//            holder.baodan.setVisibility(View.VISIBLE);

            //必带材料  预约面审设置为显示
            holder.status2.setVisibility(View.VISIBLE);
            holder.status5.setVisibility(View.VISIBLE);
            holder.status4.setText("附加材料");
            //已预约
            if (Orders.get(position).getLender() != null && Orders.get(position).getLender().getLevel() != null) {

                if (Orders.get(position).getLender().getLevel().getName().equals("A")) {
                    //A类客户
                    //评房结果
                    holder.status.setText("最高可贷" + Orders.get(position).getMaximumAmountOfCredit() + "万元；请携带");

                } else {
                    //B类客户
                    //评房结果
                    holder.status.setText("最高可贷" + Orders.get(position).getMaximumAmountOfCredit() + "万元；请携带");
//                及 附加材料  设置为显示
                    holder.status3.setVisibility(View.VISIBLE);
                    holder.status4.setVisibility(View.VISIBLE);
                }
            }
            //            必带材料
            holder.status2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LoanToast.showToastShort("必带材料", context);
//                    跳转到必带材料
                    context.startActivity(new Intent(context, TextActivity.class));

                }
            });
            //                附加材料
            holder.status4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LoanToast.showToastShort("附加材料", context);
                    description = Orders.get(position).getLender().getLevel().getDescription();
                    context.startActivity(new Intent(context, TextActivity.class).putExtra("description", description));
                }
            });
//预约
        } else if (Orders.get(position).getStage().getName().equals("预约申请已提交") ||
                Orders.get(position).getStage().getName().equals("预约已指派") || Orders.get(position).getStage().getName().equals("产调已完成") ||
                Orders.get(position).getStage().getName().equals("征信查询已完成") || Orders.get(position).getStage().getName().equals("下户已完成")) {
            holder.stage.setText("已初审");
//            holder.stage.setText("已预约");
            holder.result.setText("初审结果:");

            //必带材料  预约面审设置为显示
            holder.status2.setVisibility(View.VISIBLE);
            holder.status5.setVisibility(View.VISIBLE);
            holder.status4.setText("附加材料");
            //已预约
            if (Orders.get(position).getLender() != null && Orders.get(position).getLender().getLevel() != null) {
                if (Orders.get(position).getLender().getLevel().getName().equals("A")) {
                    //A类客户
                    //评房结果
                    holder.status.setText("最高可贷" + Orders.get(position).getMaximumAmountOfCredit() + "万元；请携带");
//
                } else {
//                //B类客户
//                //评房结果
                    holder.status.setText("最高可贷" + Orders.get(position).getMaximumAmountOfCredit() + "万元；请携带");
////                及 附加材料  设置为显示
                    holder.status3.setVisibility(View.VISIBLE);
                    holder.status4.setVisibility(View.VISIBLE);
//
                }
            }
            //            必带材料
            holder.status2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LoanToast.showToastShort("必带材料", context);
//                    跳转到必带材料
                    context.startActivity(new Intent(context, TextActivity.class));

                }
            });
            //                附加材料
            holder.status4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LoanToast.showToastShort("附加材料", context);
                    description = Orders.get(position).getLender().getLevel().getDescription();
                    context.startActivity(new Intent(context, TextActivity.class).putExtra("description", description));
                }
            });

        } else if (Orders.get(position).getStage().getName().equals("审批已完成") || Orders.get(position).getStage().getName().equals("抵押公证手续已完成") ||
                Orders.get(position).getStage().getName().equals("放款已完成")) {
            String str = Orders.get(position).getInterestPaymentMethod() == null ? "暂无付息方式" : Orders.get(position).getInterestPaymentMethod().getName();
            holder.stage.setText("已审批");
            holder.result.setText("审批结果：");
            holder.status.setText("放款金额" + Orders.get(position).getActualAmountOfCredit() +
                    "万元；" + str +
                    "；月息" + Orders.get(position).getMonthlyInterest() +
                    "%；\n返点点位" + Orders.get(position).getCommissionRate() +
                    "%；可提返点" + Orders.get(position).getCommission() + "万元。");
        } else if (Orders.get(position).getStage().getName().equals("返点已完成")) {
            holder.stage.setText("已返点");
            holder.result.setText("返点金额：");
            holder.status.setText(Orders.get(position).getCommission() + "万元");
        }

        if (Orders.get(position).getStatus().equals("Failed")) {
            holder.stage.setText("已失败");
            holder.result.setText("失败原因：");
            LoanLog.e("Orders.get(position)", Orders.get(position).toString());
            if (Orders.get(position).getCauseOfFailure() != null) {
                holder.status.setText(Orders.get(position).getCauseOfFailure().getName());
            } else {
                holder.status.setText("无");
            }
            //        将必带材料等设置为隐藏
            holder.status2.setVisibility(View.GONE);
            holder.status3.setVisibility(View.GONE);
            holder.status4.setVisibility(View.GONE);
            holder.status5.setVisibility(View.GONE);
        }

        holder.serialNumber.setText(Orders.get(position).getSerialNumber());


//        LoanLog.e("test",Utils.getLocalTimeFromUTC(Orders.get(position).getCreatedDate()));
//        LoanLog.e("test",Utils.converTime(Orders.get(position).getCreatedDate(), TimeZone.getTimeZone("GMT+8")));
        holder.createdDate.setText(Utils.converTime(Orders.get(position).getCreatedDate(), TimeZone.getTimeZone("GMT+8")));


        return convertView;
    }

    /**
     * 视图容器类
     *
     * @author kz
     */
    public static class ViewHolder {
        public TextView serialNumber;
        public TextView createdDate;
        public TextView status;
        public TextView status2;
        public TextView status3;
        public TextView status4;
        public TextView status5;
        public TextView address;
        public TextView addressleft;
        public TextView stage;
        //        评房结果
        public TextView result;
        public Button baodan;
    }

}
